const {
  querySql,
  updateSql,
  insertSql,
  deleteSql,
  queryOne,
} = require("../db");

function getFixList(query) {
  const { fixName, isSolved, pageNum, pageSize } = query;
  return querySql(
    `select fix_data.*,
    classroom_list.roomName,
    classroom_list.roomCode,
    classroom_list.space from fix_data left join classroom_list on fix_data.classroomId  = classroom_list.id where fixName like '%${
      fixName || ""
    }%' ${isSolved ? `and isSolved = ${isSolved}` : ""} limit ${
      (pageNum - 1) * pageSize
    }, ${pageSize} `
  );
}

function getTotalCount(query) {
  const { fixName, isSolved } = query;
  return queryOne(
    `SELECT COUNT(*) AS totalCount FROM fix_data where fixName like '%${
      fixName || ""
    }%' ${isSolved ? `and isSolved = ${isSolved}` : ""}`
  );
}

function editFix(query) {
  return updateSql(query, "fix_data", `where id = ${query.id}`);
}

function createFix(query) {
  return insertSql(query, "fix_data");
}

function deleteFix(query) {
  return deleteSql("id", query.id, "fix_data");
}

module.exports = {
  getFixList,
  getTotalCount,
  editFix,
  createFix,
  deleteFix,
};
